#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using MilwormsGameData;
#endregion

namespace MilwormsGame
{
    class RewardsScreen : GameScreen
    {
        public enum RewardScreenMode
        {
            Quest,
            Combat,
        };

        /// <summary>
        /// The mode of this screen.
        /// </summary>
        private RewardScreenMode mode;

        #region Rewards

        private int experienceReward;

        #endregion


        #region Graphics content

        private Texture2D backTexture;
        private Texture2D selectIconTexture;
        private Texture2D scrollUpTexture;
        private Texture2D scrollDownTexture;
        private Texture2D fadeTexture;

        private Texture2D privateTexture;
        private Texture2D privateFirstClassTexture;
        private Texture2D corporalTexture;
        private Texture2D sergeantTexture;
        private Texture2D staffSergeantTexture;
        private Texture2D masterSergeantTexture;

        private Vector2 backgroundPosition;
        private Vector2 textPosition;
        private Vector2 iconPosition;
        private Vector2 selectPosition;
        private Vector2 selectIconPosition;
        private Vector2 screenSize;
        private Vector2 titlePosition;
        private Vector2 scrollUpPosition;
        private Vector2 scrollDownPosition;
        private Vector2 xpAwardPosition;
        private Vector2 rankAwardPosition;
        private Rectangle fadeDest;

        #endregion


        #region Dialog Text


        private string titleText;
        private readonly string selectString = "Continue";


        #endregion


        #region Scrollable List Data


        /// <summary>
        /// Starting index of the list to be displayed
        /// </summary>
        private int startIndex;

        /// <summary>
        /// Ending index of the list to be displayed
        /// </summary>
        private int endIndex;

        /// <summary>
        /// Maximum number of lines to draw in the screen
        /// </summary>
        private int maxLines;

        /// <summary>
        /// Vertical spacing between each line
        /// </summary>
        private float lineSpacing;


        #endregion


        #region Initialization


        /// <summary>
        /// Creates a new RewardsScreen object.
        /// </summary>
        public RewardsScreen(RewardScreenMode mode, int experienceReward)
            : base()
        {
            this.IsPopup = true;
            this.mode = mode;
            this.experienceReward = experienceReward;

            maxLines = 3;
            lineSpacing = 74 * ScaledVector2.ScaleFactor;

            startIndex = 0;
            endIndex = maxLines;

            // play the appropriate music
            switch (mode)
            {
                case RewardScreenMode.Combat:
                    // play the combat-victory music
                    //AudioManager.PushMusic("WinTheme", false);
                    break;

                case RewardScreenMode.Quest:
                    // play the quest-complete music
                    AudioManager.PushMusic("RewardTheme", false);
                    break;
            }

            this.Exiting += new EventHandler(RewardsScreen_Exiting);
        }


        void RewardsScreen_Exiting(object sender, EventArgs e)
        {
            AudioManager.PopMusic();
        }

        /// <summary>
        /// Load the graphics content from the content manager.
        /// </summary>
        public override void LoadContent()
        {
            ContentManager content = ScreenManager.Game.Content;
            backTexture =
                content.Load<Texture2D>(@"Textures\GameScreens\PopupScreenNone");
            selectIconTexture =
                content.Load<Texture2D>(@"Textures\Buttons\rpgbtn");
            scrollUpTexture =
                content.Load<Texture2D>(@"Textures\GameScreens\ScrollUp");
            scrollDownTexture =
                content.Load<Texture2D>(@"Textures\GameScreens\ScrollDown");
            fadeTexture = content.Load<Texture2D>(@"Textures\GameScreens\FadeScreen");

            privateTexture =
                content.Load<Texture2D>(@"Textures\GameScreens\private");
            privateFirstClassTexture =
                content.Load<Texture2D>(@"Textures\GameScreens\privateFirstClass");
            corporalTexture =
                content.Load<Texture2D>(@"Textures\GameScreens\corporal");
            sergeantTexture =
                content.Load<Texture2D>(@"Textures\GameScreens\sergeant");
            staffSergeantTexture =
                content.Load<Texture2D>(@"Textures\GameScreens\staffSergeant");
            masterSergeantTexture =
                content.Load<Texture2D>(@"Textures\GameScreens\masterSergeant");

            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
            fadeDest = new Rectangle(viewport.X, viewport.Y, viewport.Width,
                viewport.Height);
            backgroundPosition.X = (viewport.Width - backTexture.Width * ScaledVector2.DrawFactor) / 2;
            backgroundPosition.Y = (viewport.Height - backTexture.Height * ScaledVector2.DrawFactor) / 2;

            screenSize = new Vector2(viewport.Width, viewport.Height);

            selectIconPosition.X = screenSize.X / 2 + 120 * ScaledVector2.ScaleFactor;
            selectIconPosition.Y = backgroundPosition.Y + 520f * ScaledVector2.ScaleFactor;
            selectPosition.X = selectIconPosition.X -
                Fonts.ButtonNamesFont.MeasureString(selectString).X - 10f * ScaledVector2.ScaleFactor;
            selectPosition.Y = backgroundPosition.Y + 530f * ScaledVector2.ScaleFactor;

            textPosition = backgroundPosition + ScaledVector2.GetScaledVector(335f, 320f);
            iconPosition = backgroundPosition + ScaledVector2.GetScaledVector(155f, 303f);

            scrollUpPosition = backgroundPosition + ScaledVector2.GetScaledVector(810f, 300f);
            scrollDownPosition = backgroundPosition + ScaledVector2.GetScaledVector(810f, 480f);

            xpAwardPosition = backgroundPosition + ScaledVector2.GetScaledVector(170f, 240f);
            rankAwardPosition = backgroundPosition + ScaledVector2.GetScaledVector(170f, 300f);
        }


        #endregion


        #region Updating


        /// <summary>
        /// Handles user input.
        /// </summary>
        public override void HandleInput()
        {
            bool backClicked = false;

            if (InputManager.IsButtonClicked(new Rectangle(
                (int)selectIconPosition.X, (int)selectIconPosition.Y,
                selectIconTexture.Width, selectIconTexture.Height)))
            {
                backClicked = true;
            }

            // exit the screen
            if (backClicked ||
                InputManager.IsActionTriggered(InputManager.Action.Back))
            {
                ExitScreen();
                // give the rewards to the party
                Session.Party.GiveExperience(experienceReward);

            }
            // Scroll up
            else if (InputManager.IsActionTriggered(InputManager.Action.CursorUp))
            {
                if (startIndex > 0)
                {
                    startIndex--;
                    endIndex--;
                }
            }
            // Scroll down
            else if (InputManager.IsActionTriggered(InputManager.Action.CursorDown))
            {
                if (startIndex < maxLines)
                {
                    endIndex++;
                    startIndex++;
                }
            }
        }


        #endregion


        #region RewardsInsignia

        private String CheckRankDesc(int xp)
        {
            String rank = "Private";

            switch (xp)
            {
                case 0: rank = "Private"; break;
                case 20: rank = "Private First Class"; break;
                case 40: rank = "Corporal"; break;
                case 60: rank = "Sergeant"; break;
                case 80: rank = "Staff Sergeant"; break;
                case 100: rank = "Master Sergeant"; break;
            }

            return rank;
        }

        #endregion


        #region Drawing


        /// <summary>
        ///  Draw the screen.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            Vector2 currentIconPosition = iconPosition;
            Vector2 currentTextPosition = textPosition;

            switch (mode)
            {
                case RewardScreenMode.Quest:
                    titleText = "Quest Complete";
                    break;

                case RewardScreenMode.Combat:
                    titleText = "Combat Won";
                    break;
            }
            titlePosition.X = (screenSize.X -
                Fonts.HeaderFont.MeasureString(titleText).X) / 2;
            titlePosition.Y = backgroundPosition.Y + lineSpacing;

            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            spriteBatch.Begin();

            // Draw the fading screen
            spriteBatch.Draw(fadeTexture, new Vector2(ScreenManager.GraphicsDevice.Viewport.Width,
                ScreenManager.GraphicsDevice.Viewport.Height), null, Color.White, 0f,
                Vector2.Zero, ScaledVector2.DrawFactor, SpriteEffects.None, 0f);

            // Draw the popup background
            spriteBatch.Draw(backTexture, backgroundPosition, null, Color.White, 0f,
                Vector2.Zero, ScaledVector2.DrawFactor, SpriteEffects.None, 0f);

            // Draw the title
            spriteBatch.DrawString(Fonts.HeaderFont, titleText, titlePosition,
                Fonts.TitleColor);

            // Draw the experience points awarded
            spriteBatch.DrawString(Fonts.GearInfoFont,
                "You are promoted to :        " + CheckRankDesc(experienceReward),
                xpAwardPosition, Fonts.CountColor);

            if (experienceReward == 0)
            {
                spriteBatch.Draw(privateTexture, rankAwardPosition, null, Color.White, 0f,
                Vector2.Zero, ScaledVector2.DrawFactor, SpriteEffects.None, 0f);
            }
            if (experienceReward == 20)
            {
                spriteBatch.Draw(privateFirstClassTexture, rankAwardPosition, null, Color.White, 0f,
                Vector2.Zero, ScaledVector2.DrawFactor, SpriteEffects.None, 0f);
            }
            if (experienceReward == 40)
            {
                spriteBatch.Draw(corporalTexture, rankAwardPosition, null, Color.White, 0f,
                Vector2.Zero, ScaledVector2.DrawFactor, SpriteEffects.None, 0f);
            }
            if (experienceReward == 60)
            {
                spriteBatch.Draw(sergeantTexture, rankAwardPosition, null, Color.White, 0f,
                Vector2.Zero, ScaledVector2.DrawFactor, SpriteEffects.None, 0f);
            }
            if (experienceReward == 80)
            {
                spriteBatch.Draw(staffSergeantTexture, rankAwardPosition, null, Color.White, 0f,
                Vector2.Zero, ScaledVector2.DrawFactor, SpriteEffects.None, 0f);
            }
            if (experienceReward == 100)
            {
                spriteBatch.Draw(masterSergeantTexture, rankAwardPosition, null, Color.White, 0f,
                Vector2.Zero, ScaledVector2.DrawFactor, SpriteEffects.None, 0f);
            }

            // Draw the item details
            for (int i = startIndex; i < endIndex; i++)
            {
                // Increment the position to the next line
                currentTextPosition.Y += lineSpacing;
                currentIconPosition.Y += lineSpacing;
            }
            // Draw the scroll buttons
            spriteBatch.Draw(scrollUpTexture, scrollUpPosition, null, Color.White, 0f,
                Vector2.Zero, ScaledVector2.DrawFactor, SpriteEffects.None, 0f);
            spriteBatch.Draw(scrollDownTexture, scrollDownPosition, null, Color.White, 0f,
                Vector2.Zero, ScaledVector2.DrawFactor, SpriteEffects.None, 0f);

            // Draw the select button and its corresponding text
            spriteBatch.Draw(selectIconTexture, selectIconPosition, null, Color.White, 0f,
                Vector2.Zero, ScaledVector2.DrawFactor, SpriteEffects.None, 0f);

            Vector2 selectFontPosition = Fonts.GetCenterPositionInButton(Fonts.ButtonNamesFont, selectString,
                new Rectangle((int)selectIconPosition.X, (int)selectIconPosition.Y,
                    selectIconTexture.Width, selectIconTexture.Height));


            spriteBatch.DrawString(Fonts.ButtonNamesFont, selectString, selectFontPosition,
                Color.White);
            spriteBatch.End();
        }


        #endregion
    }
}
